package com.iexbuy.ihg.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.iexbuy.ihg.R;
import com.iexbuy.ihg.entity.CashTiXianListObject;
import com.iexbuy.ihg.entity.CoinHistoryListObject;
import com.iexbuy.ihg.net.ServiceHelper;
import com.iexbuy.ihg.utils.JsonUtils;
import com.jingchen.pulltorefresh.PullToRefreshLayout;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.HashMap;

public class WeDeCoinRecordDetailFragment extends BaseFragment {

	private static WeDeCoinRecordDetailFragment homeFragmentInstent;

	@ViewInject(R.id.msgListLayout)
	private PullToRefreshLayout msgListRefreshLayout;
	@ViewInject(R.id.itemType)
	private TextView itemType;
	@ViewInject(R.id.changeCoinAmount)
	private TextView changeCoinAmount;
	@ViewInject(R.id.itemTime)
	private TextView itemTime;
	@ViewInject(R.id.itemName)
	private TextView itemName;

	private static CoinHistoryListObject coinHistoryListObject;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		homeFragmentInstent = this;
	}

	@Override
	protected View _onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_wede_coin_record_detail, container, false);
		ViewUtils.inject(this, rootView);
		initView(rootView);
		return rootView;
	}

	private void initView(View rootView) {
		mBaseActivity.setTitle(getResources().getString(R.string.we_de_coins_detail));
		mBaseActivity.setTitleLayoutVisibility(true);
		mBaseActivity.setHomeActivity(false);

		getDetailData();
	}

	private void getDetailData() {
		HashMap<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("historyId", coinHistoryListObject.getId());
		ServiceHelper.send(mBaseActivity, ServiceHelper.GET_COIN_HISTORY_DETAIL, paramMap,
				new RequestCallBack<String>() {
					@Override
					public void onStart() {
					}

					@Override
					public void onCancelled() {
					}

					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						coinHistoryListObject = JsonUtils.parseJsonStrToObject(JsonUtils.getJsonSubStringByKey(responseInfo.result, "body"), CoinHistoryListObject.class);
						initView();
					}

					@Override
					public void onFailure(HttpException error, String msg) {
					}
				});
	}

	private void initView() {
		if (coinHistoryListObject != null) {
			itemType.setText(coinHistoryListObject.getTypeName());
			changeCoinAmount.setText(coinHistoryListObject.getDelta());
			itemTime.setText(coinHistoryListObject.getTime());
			itemName.setText(coinHistoryListObject.getTypeName());
		}
	}

	public static void setCoinHistoryListObject(CoinHistoryListObject coinHistoryListObject) {
		WeDeCoinRecordDetailFragment.coinHistoryListObject = coinHistoryListObject;
	}

	/**
	 * @return the homeFragmentInstent
	 */
	public static WeDeCoinRecordDetailFragment getHomeFragmentInstent() {
		return homeFragmentInstent;
	}

}
